﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SJ.Platform.Extend.IOC.CurrentUser;
using SJ.Platform.Common;
using SJ.Platform.EFCore.Model;
using SJ.Platform.EFCore.Model.RoleManager;
using SJ.Platform.Extend.Request;
using SJ.Platform.Extend.Response;
using SJ.Platform.EFCore.DBOperation.DBOperation.MySQL.Sys;

namespace SJ.Platform.Api.Controllers.RoleManager
{
    /// <summary>
    /// 角色操作接口
    /// </summary>
    [Route(GlobalVariableHandle.Scope_Api + "/Role")]
    [ApiController]
    [Authorize(GlobalVariableHandle.Scope_Api)]
    public class RoleManagerController : ControllerBase
    {
        private readonly ICurrentUser _currentUser;
        private readonly Sys_RoleHandle sys_RoleHandle;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext">数据库</param>
        /// <param name="currentUser">当前用户</param>
        public RoleManagerController(SJDBContext dbContext, ICurrentUser currentUser)
        {
            _currentUser = currentUser;
            sys_RoleHandle = new Sys_RoleHandle(dbContext);
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("CreateRoleAsync")]
        public async Task<IActionResult> CreateRoleAsync([FromBody] SysRole model)
        {
            var role = new SysRole
            {
                Id = Guid.NewGuid().ToString(),
                RoleName = model.RoleName,
                AddTime = DateTime.Now,
                AddUserId = _currentUser.UserId,
                EditTime = DateTime.Now,
                EditUserId = _currentUser.UserId
            };
            try
            {
                bool result = await sys_RoleHandle.CreateAsync(role);
                return Ok(ApiNormalResponse<bool>.SetResponse(result ? ApiResponseStatus.Success : ApiResponseStatus.BadRequest, "角色添加" + (result ? "成功" : "失败"), result));
            }
            catch (Exception ex)
            {
                // 处理异常情况
                return BadRequest(ApiNormalResponse<bool>.SetResponse(ApiResponseStatus.BadRequest, "角色添加失败：" + ex.Message, false));
            }
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("UpdateRoleAsync")]
        public async Task<IActionResult> UpdateRoleAsync([FromBody] SysRole model)
        {
            var role = new SysRole
            {
                Id = model.Id,
                RoleName = model.RoleName,
                AddTime = model.AddTime,
                AddUserId = model.AddUserId,
                EditTime = DateTime.Now,
                EditUserId = _currentUser.UserId
            };
            try
            {
                bool result = await sys_RoleHandle.UpdateAsync(role);
                return Ok(ApiNormalResponse<bool>.SetResponse(result ? ApiResponseStatus.Success : ApiResponseStatus.BadRequest, "角色修改" + (result ? "成功" : "失败"), result));
            }
            catch (Exception ex)
            {
                // 处理异常情况
                return BadRequest(ApiNormalResponse<bool>.SetResponse(ApiResponseStatus.BadRequest, "角色修改失败：" + ex.Message, false));
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet("DeleteByRoleIdAsync/{roleId}")]
        public async Task<IActionResult> DeleteByRoleIdAsync(string roleId)
        {
            try
            {
                bool result = await sys_RoleHandle.DeleteByIdAsync(roleId);
                return Ok(ApiNormalResponse<bool>.SetResponse(result ? ApiResponseStatus.Success : ApiResponseStatus.BadRequest, "删除角色" + (result ? "成功" : "失败"), result));
            }
            catch (Exception ex)
            {
                // 处理异常情况
                return BadRequest(ApiNormalResponse<bool>.SetResponse(ApiResponseStatus.BadRequest, "删除角色失败：" + ex.Message, false));
            }
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("GetAllRoleAsync")]
        public async Task<IActionResult> GetAllRoleAsync([FromBody] ApiPageQueryRequest<SysRole> request)
        {
            try
            {
                var result = await sys_RoleHandle.GetAllAsync((int)request.PageIndex, (int)request.PageSize, ApiPageQueryRequest<SysRole>.GenerateCondition(request.Condition));

                return Ok(ApiPageQueryResponse<List<SysRole>>.SetResponse(ApiResponseStatus.Success, "查询成功", (int)request.PageIndex, (int)request.PageSize, result.count, result.roles)); // 返回成功的响应
            }
            catch (Exception ex)
            {
                // 处理异常情况
                return BadRequest(ApiPageQueryResponse<List<SysRole>>.SetResponse(ApiResponseStatus.BadRequest, "查询失败：" + ex.Message, (int)request.PageIndex, (int)request.PageSize, 0, new List<SysRole> { }));
            }
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAllRoleAsync")]
        public async Task<IActionResult> GetAllRoleAsync()
        {
            try
            {
                var result = await sys_RoleHandle.GetAllAsync();
                return Ok(ApiNormalResponse<List<SysRole>>.SetResponse(ApiResponseStatus.Success, "查询成功", result)); // 返回成功的响应
            }
            catch (Exception ex)
            {
                // 处理异常情况
                return BadRequest(ApiNormalResponse<List<SysRole>>.SetResponse(ApiResponseStatus.BadRequest, "查询失败：" + ex.Message, new List<SysRole> { }));
            }
        }
    }
}
